package models

import (
	"encoding/json"
	"errors"
	"time"
)

//权限结构
type CasbinModel struct {
	UserID   int64  `json:"user_id" db:"user_id"`
	Ptype    string `json:"ptype" db:"ptype"`
	RoleName string `json:"rolename" db:"v0"`
	Path     string `json:"path" db:"v1"`
	Method   string `json:"method" db:"v2"`
}

type LoginUser struct {
	UserID      uint64    `json:"user_id" db:"user_id"`
	PhoneNumber uint64    `json:"phone_number" db:"phone_number"`
	UserName    string    `json:"username" db:"username"`
	Password    string    `json:"password" db:"password"`
	Email       string    `json:"email" db:"email"`
	CreatedAt   time.Time `db:"created_at"`
	UpdatedAt   time.Time `db:"updated_at"`
}

func (L *LoginUser) UnmarshalJSON(data []byte) (err error) {
	required := struct {
		UserName string `json:"username" db:"username"`
		Password string `json:"password" db:"password"`
	}{}
	err = json.Unmarshal(data, &required)
	if err != nil {
		return
	} else if len(required.UserName) == 0 {
		err = errors.New("缺少必填字段username")
	} else if len(required.Password) == 0 {
		err = errors.New("缺少必填字段password")
	} else {
		L.UserName = required.UserName
		L.Password = required.Password
	}
	return
}

type User struct {
	UserID      uint64    `json:"user_id" db:"user_id"`
	PhoneNumber uint64    `json:"phone_number" db:"phone_number"`
	UserName    string    `json:"username" db:"username"`
	Password    string    `json:"password" db:"password"`
	Email       string    `json:"email" db:"email"`
	CreatedAt   time.Time `db:"created_at"`
	UpdatedAt   time.Time `db:"updated_at"`
}

func (u *User) UnmarshalJSON(data []byte) (err error) {
	required := struct {
		PhoneNumber uint64 `json:"phone_number" db:"phone_number"`
		UserName    string `json:"username" db:"username"`
		Password    string `json:"password" db:"password"`
		Email       string `json:"email" db:"email"`
	}{}
	err = json.Unmarshal(data, &required)
	if err != nil {
		return
	} else if required.PhoneNumber == 0 {
		err = errors.New("缺少必填字段phone_number")
	} else if len(required.UserName) == 0 {
		err = errors.New("缺少必填字段username")
	} else if len(required.Password) == 0 {
		err = errors.New("缺少必填字段password")
	} else if len(required.Email) == 0 {
		err = errors.New("缺少必填字段email")
	} else {
		u.PhoneNumber = required.PhoneNumber
		u.UserName = required.UserName
		u.Password = required.Password
		u.Email = required.Email
	}
	return
}

type RegisterForm struct {
	PhoneNumber     uint64 `json:"phone_number"`
	UserName        string `json:"username"`
	Password        string `json:"password"`
	ConfirmPassword string `json:"confirm_password"`
	Email           string `json:"email"`
}

func (r *RegisterForm) UnmarshalJSON(data []byte) (err error) {
	required := struct {
		PhoneNumber     uint64 `json:"phone_number"`
		UserName        string `json:"username"`
		Password        string `json:"password"`
		ConfirmPassword string `json:"confirm_password"`
		Email           string `json:"email"`
	}{}
	err = json.Unmarshal(data, &required)
	if err != nil {
		return
	} else if required.PhoneNumber == 0 {
		err = errors.New("缺少必填字段phone_number")
	} else if len(required.UserName) == 0 {
		err = errors.New("缺少必填字段username")
	} else if len(required.Password) == 0 {
		err = errors.New("缺少必填字段password")
	} else if required.Password != required.ConfirmPassword {
		err = errors.New("两次密码不一致")
	} else if len(required.Email) == 0 {
		err = errors.New("缺少必填字段email")
	} else {
		r.PhoneNumber = required.PhoneNumber
		r.UserName = required.UserName
		r.Password = required.Password
		r.ConfirmPassword = required.ConfirmPassword
		r.Email = required.Email
	}
	return
}
